#ifndef HARDINFOR_H
#define HARDINFOR_H

#include <QObject>
#include <mutex>
#include <unordered_map>
#include "mytypes.h"
class HardInfor : public QObject
{
    Q_OBJECT
public:
    //功能以静态方式实现
    static HardInfor* GetInstance(QObject* parent=nullptr);
public:
    ~HardInfor();

    template<typename T>
    void SetNormalCheck(Yzty::HardToolCode modelindex,std::vector<T>& infors);         //设置信息

    const std::vector<Yzty::BaceInfor*> GetNormalCheck(Yzty::HardToolCode modelindex);        //读取信息

    void Clear();                                                           //清空保存信息
private:
    explicit HardInfor(QObject* parent=nullptr);

private:
    std::mutex _mutex;
    static HardInfor* _instance;
    std::unordered_map<Yzty::HardToolCode,std::vector<Yzty::BaceInfor*>>  _infors;
};

template <typename T>
void HardInfor::SetNormalCheck(Yzty::HardToolCode modelindex,std::vector<T>& infors)
{
    std::lock_guard<std::mutex> guard(_mutex);

    int n=int(_infors[modelindex].size());
    int m=infors.size();

    //内存维护
    if(n<m)
        for(int i=n;i<m;++i)
            _infors[modelindex].push_back(new T(uint16_t(modelindex)));
    else if(n>m)
    {
        for(int i=m;i<n;++i)
            delete _infors[modelindex][size_t(i)];
        _infors[modelindex].erase(_infors[modelindex].begin()+m,_infors[modelindex].end());
    }

    for(int i=0;i<m;++i)
        *dynamic_cast<T*>(_infors[modelindex][size_t(i)])=std::move(infors[size_t(i)]);
    //先向下转换，在向下进行移动赋值
}

#endif // HARDINFOR_H
